package com.xxl.job.core.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static cn.hutool.core.convert.Convert.toHex;

/**
 * @author xieshuang
 * @since 2023/4/5 16:37
 */
public class MD5 {

    public static MessageDigest md;

    static {
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String md5(String str) {
        byte[] bytes = md.digest(str.getBytes());
        return toHex(bytes);
    }

    public static String md5(File str) {
        byte[] bytes = md.digest(IoUtil.readBytes(FileUtil.getInputStream(str)));
        return toHex(bytes);
    }
}
